﻿using System;
using Willow.IRepository;
using Willow.Model;
using Willow.Authorization;
using Willow.IService.Auth;
using Willow.IRepository.Auth;
using System.Net;
using Willow.Common;
using Willow.Extensions;
namespace Willow.Service.Auth
{
    public class UserService : BaseService<Auth_User>, IUserService
    {

        IUserRepository UserRepository { get; set; }
        IRepository<Sys_LoginLog> LoginLogRepository { get; set; }

        public UserService(IUserRepository repository, IUnitOfWork unitOfWork)
            : base(repository, unitOfWork)
        {
            UserRepository = repository;
            LoginLogRepository = unitOfWork.GetStandardRepository<Sys_LoginLog>();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="TObject">The t object.</param>
        public override void Save(Auth_User TObject)
        {
            TObject.Spell = TObject.FullName.GetFirstPinyin();
            base.Save(TObject);
        }

        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids">The ids.</param>
        public void BatchLogicalDelete(Guid[] ids)
        {
            foreach (var id in ids)
            {
                var user = this.Find(id);
                if (user == null)
                {
                    continue;
                }
                user.IsDelete = true;
                this.Update(user);
            }
            UnitOfWork.SaveChanges();
        }

        /// <summary>
        /// 登陆系统
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <exception cref="System.Exception">用户名或密码错误</exception>
        public void Login(string username, string password)
        {
            var user = FirstOrDefault(p => p.UserName == username && p.Password == password);
            if (user == null)
            {
                AddLoginLog(username, "用户名或密码错误", false);
                throw new WarningException("用户名或密码错误");
            }

            var loginUser = UserRepository.GetLoginUser(user.Id);
            loginUser.IPAddress = NetHelper.GetRequestIP();
            OnlineContext.Instance.Add(loginUser);
            AddLoginLog(username, "登陆成功", true);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="oldPassword">The old password.</param>
        /// <param name="newPassword">The new password.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public void ChangePassword(Guid userId, string oldPassword, string newPassword)
        {
            var user = FirstOrDefault(p => p.Id == userId);
            if (user == null)
            {
                throw new WarningException("用户不存在！");
            }
            if (user.Password != oldPassword)
            {
                throw new WarningException("原始密码错误！");
            }
            user.Password = newPassword;
            base.Update(user);
            UnitOfWork.SaveChanges();
        }

        /// <summary>
        /// 记录登陆日志
        /// </summary>
        /// <param name="remark">The remark.</param>
        /// <param name="successful">if set to <c>true</c> [successful].</param>
        private void AddLoginLog(string username, string remark, bool successful)
        {
            string ip = NetHelper.GetRequestIP();
            LoginLogRepository.Create(new Sys_LoginLog
            {
                Id = Guid.NewGuid(),
                LoginTime = DateTime.Now,
                Remark = remark,
                Successful = successful,
                UserName = username,
                Ip = ip
            });
            UnitOfWork.SaveChanges();
        }

    }
}
